package com.example.forestmanagement.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.forestmanagement.R;
import com.example.forestmanagement.model.UserInfo;
import java.util.List;

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder> {
    private List<UserInfo> userList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(UserInfo user);
    }

    public UserListAdapter(List<UserInfo> userList, OnItemClickListener listener) {
        this.userList = userList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserInfo user = userList.get(position);
        holder.tvName.setText(user.getUserName());
        holder.tvJob.setText(user.getJob() + " - " + user.getJobGroup());
        
        // 设置在线状态
        if (user.getIsAlive() == 1) {
            holder.tvStatus.setText("在线");
            holder.tvStatus.setTextColor(0xFF4CAF50);
        } else {
            holder.tvStatus.setText("离线");
            holder.tvStatus.setTextColor(0xFFFF5722);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(user);
            }
        });
    }

    @Override
    public int getItemCount() {
        return userList != null ? userList.size() : 0;
    }

    public void updateData(List<UserInfo> newUserList) {
        this.userList = newUserList;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tvName;
        TextView tvJob;
        TextView tvStatus;

        ViewHolder(View view) {
            super(view);
            ivAvatar = view.findViewById(R.id.iv_avatar);
            tvName = view.findViewById(R.id.tv_name);
            tvJob = view.findViewById(R.id.tv_job);
            tvStatus = view.findViewById(R.id.tv_status);
        }
    }
} 